package com.pingsky.apm.agent.api.util;

import java.io.File;
import java.io.FileFilter;

public class FileUtil {
    public static File fileExist(String parentDirStr, String fileName) {
        File parentDir = new File(parentDirStr);
        if (parentDir.exists()) {
            File file = new File(parentDir, fileName);
            if (file.exists()) {
                return file;
            } else {
                throw new RuntimeException(String.format("no such file:%s in %s", fileName, parentDir.getAbsolutePath()));
            }
        } else {
            throw new RuntimeException(String.format("no parent file: %s", parentDirStr));
        }
    }

    public static File[] listFiles(final File path, final String[] fileExtensions) {
        Assert.requireNonNull(path, "path");
        Assert.requireNonNull(fileExtensions, "fileExtensions");

        return path.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                String path = pathname.getName();
                for (String extension : fileExtensions) {
                    if (path.lastIndexOf(extension) != -1) {
                        return true;
                    }
                }
                return false;
            }
        });
    }
}
